#include "seq.h"

#include <stdio.h>
#include <assert.h>
#include <string.h>

static void
test()
{
    Seq_T seq = Seq_new(0);
    assert(Seq_length(seq) == 0);
    Seq_free(&seq);

    seq = Seq_seq("hello", "how", "are", NULL);
    assert(Seq_length(seq) == 3);
    assert(strcmp(Seq_get(seq, 0), "hello") == 0);
    assert(strcmp(Seq_get(seq, 2), "are") == 0);

    Seq_put(seq, 2, "you");
    assert(strcmp(Seq_get(seq, 2), "you") == 0);
    Seq_free(&seq);
}

static void
test_get_put()
{
    Seq_T seq = Seq_new(0);
    Seq_addlo(seq, (void*)100);
    Seq_addlo(seq, (void*)200);

    assert(Seq_length(seq) == 2);
    assert(Seq_get(seq, 0) == (void*)200);
    assert(Seq_get(seq, 1) == (void*)100);
    Seq_free(&seq);

    seq = Seq_new(0);
    Seq_addhi(seq, (void*)100);
    Seq_addhi(seq, (void*)200);

    assert(Seq_length(seq) == 2);
    assert(Seq_get(seq, 0) == (void*)100);
    assert(Seq_get(seq, 1) == (void*)200);
    Seq_free(&seq);

    seq = Seq_new(0);
    Seq_addhi(seq, (void*)100);
    Seq_addlo(seq, (void*)200);

    assert(Seq_length(seq) == 2);
    assert(Seq_get(seq, 0) == (void*)200);
    assert(Seq_get(seq, 1) == (void*)100);
    Seq_free(&seq);
}

static void
test_get_put2()
{
    Seq_T seq = Seq_new(0);
    int i;
    for (i = 0; i < 15; i++)
        Seq_addhi(seq, (void*)(size_t)i);
    for (; i < 20; i++)
        Seq_addlo(seq, (void*)(size_t)i);

    assert(Seq_length(seq) == 20);
    assert(Seq_get(seq, 0) == (void*)19);
    assert(Seq_get(seq, 19) == (void*)14);
    Seq_free(&seq);
}

static void
test_remove()
{
    Seq_T seq = Seq_new(0);
    Seq_addhi(seq, (void*)100);
    Seq_addhi(seq, (void*)200);
    Seq_addhi(seq, (void*)300);
    Seq_addhi(seq, (void*)400);

    assert(Seq_remlo(seq) == (void*)100);
    assert(Seq_remhi(seq) == (void*)400);
    assert(Seq_remlo(seq) == (void*)200);
    assert(Seq_remlo(seq) == (void*)300);
    assert(Seq_length(seq) == 0);

    for (int i = 0; i < 20; i++)
        Seq_addlo(seq, (void*)(size_t)i);
    assert(Seq_length(seq) == 20);
    assert(Seq_get(seq, 0) == (void*)19);
    assert(Seq_get(seq, 19) == (void*)0);
    Seq_free(&seq);
}

int main()
{
    test();
    test_get_put();
    test_get_put2();
    test_remove();
    return 0;
}

